<template>
  <div id="app" v-cloak>
    <!-- 骨架屏 -->
    <div v-if="loading" class="skeleton-container">
      <div class="skeleton-banner"></div>
      <div class="skeleton-inquiry"></div>
      <div class="skeleton-project"></div>
      <div class="skeleton-service"></div>
    </div>
    <div v-if="!loading" class="main-content">
      <!-- 轮播图部分 -->
      <HomeBanner :banner-data="banner_box" />

      <!-- 满足您需求的服务 -->
      <div class="services-container">
        <div class="tit-line">
          <span class="tit-line1"></span>
          <span class="tit-line2"></span>
        </div>
        <h2 class="solve-tit">{{$t('contentDetails.ServicesNeed')}}</h2>
        <!-- 满足您需求的服务 菜单栏 -->
        <div class="services-layout">
          <div class="services-list">
            <div
              v-for="(service, index) in services"
              :key="index"
              class="service-item"
              :class="{ active: activeService === index }"
              @click="navigateToService(index)"
            >
              <h3 class="service-title">{{ service.title }}</h3>
<!--              <div class="service-en-title">{{ service.subTitle }}</div>-->
            </div>
          </div>
        </div>
      </div>

      <router-view/>
    </div>
  </div>
</template>

<script>
import {bannerList} from "@/api/banner/banner"
import HomeBanner from "../components/home/HomeBanner.vue";

export default {
  components: {HomeBanner},
  data() {
    return {
      loading: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      banner_box: [],
      activeService: 0,
      services: [
        {title: this.$t('navDetails.Warehousing'), subTitle: "Storage & Distribution", path: '/services/warehousing'},
        {title: this.$t('navDetails.Customs'), subTitle: "Customs Clearance", path: '/services/customs'},
        {title: this.$t('navDetails.Ecommerce'), subTitle: "E-Economics", path: '/services/ecommerce'},
        {title: this.$t('navDetails.Oversize'), subTitle: "Heavy & Oversized Cargo Handling", path: '/services/oversize'},
        {title: this.$t('navDetails.ProjectCargo'), subTitle: "Project Cargo Logistics", path: '/services/project-cargo'},
      ],
      serviceRoutes: {
        '/services/warehousing': 0,
        '/services/customs': 1,
        '/services/ecommerce': 2,
        '/services/oversize': 3,
        '/services/project-cargo': 4
      }
    }
  },
  async created() {
    await this.getBannerList();
    this.setActiveServiceFromRoute();
  },
  watch: {
    '$route'(to) {
      this.setActiveServiceFromRoute();
    }
  },
  methods: {
    /** 查询轮播图 */
    getBannerList() {
      this.loading = true;
      bannerList(6).then(response => {
        if (response.data) {
          this.banner_box = response.data;
          // Ensure image URL is properly set
          if (!this.banner_box.imageUrl) {
            console.warn('Banner image URL is empty');
          }
        }
        this.loading = false;
      }).catch(error => {
        console.error('Error loading banner:', error);
        this.loading = false;
      });
    },

    // 轮播图跳转
    handleRedirect() {
      if (this.banner_box.buttonLink) {
        this.$router.push(this.banner_box.buttonLink);
      }
    },

    setActiveServiceFromRoute() {
      const currentPath = this.$route.path;
      if (this.serviceRoutes.hasOwnProperty(currentPath)) {
        this.activeService = this.serviceRoutes[currentPath];
      }
    },

    navigateToService(index) {
      if (index >= 0 && index < this.services.length) {
        this.activeService = index;
        this.$router.push(this.services[index].path);
      }
    }
  }
}
</script>

<style scoped lang="scss">
[v-cloak] {
  display: none;
}

.skeleton-container {
  width: 100%;
  min-height: 2000px;
}

/* 骨架屏动画 */
.skeleton-banner {
  height: 800px;
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}

.services-container {
  max-width: 1200px;
  margin: 80px auto 0;
  padding: 0 20px;
}

.services-layout {
  margin-top: 40px;
  overflow: hidden;
}

.services-list {
  display: flex;
  justify-content: space-between;
  padding: 0;
  margin: 0;
  list-style: none;
  border: 1px solid #e0e0e0;
  overflow: hidden;
  user-select: none;
  cursor: pointer;
}

.tit-line {
  display: flex;
  justify-content: left;
  margin-bottom: 20px;

  .tit-line1, .tit-line2 {
    height: 8px;
    background: #fe3231;
    border-radius: 10px;
  }

  .tit-line1 {
    width: 60px;
  }

  .tit-line2 {
    width: 10px;
    margin-left: 10px;
  }
}

.solve-tit {
  font-size: 30px;
  font-weight: bold;
  color: #333;
  margin-bottom: 15px;
  text-align: left;
}

.service-item {
  flex: 1;
  padding: 20px;
  text-align: center;
  background-color: #eeeeee;
  border-right: 1px solid #e0e0e0;
  transition: all 0.3s ease;

  &:last-child {
    border-right: none;
  }

  &:hover {
    background-color: #f5f5f5;
  }

  &.active {
    background-color: #fe3231;
    color: #fff;
    border-right-color: #c72c2c;

    .service-title {
      color: #fff;
    }
  }
}

.service-title {
  font-size: 18px;
  font-weight: 550;
  color: #333;
  margin-bottom: 5px;
  transition: color 0.3s;
}

.service-en-title {
  font-size: 14px;
  color: #666;
}

/* 动画效果 */
@keyframes shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* =============== 移动端适配优化 =============== */
@media (max-width: 992px) {
  .services-container {
    margin-top: 60px;
  }

  .solve-tit {
    font-size: 26px;
  }

  .service-item {
    padding: 15px 10px;
  }

  .service-title {
    font-size: 16px;
  }

  .service-en-title {
    font-size: 12px;
  }
}

@media (max-width: 768px) {
  .services-container {
    margin-top: 40px;
    padding: 0 15px;
  }

  .tit-line {
    margin-bottom: 15px;
  }

  .solve-tit {
    font-size: 22px;
    margin-bottom: 10px;
  }

  .services-layout {
    margin-top: 20px;
  }

  .services-list {
    flex-direction: column;
    border: none;
  }

  .service-item {
    padding: 14px;
    margin-bottom: 8px;
    border: 1px solid #dcdcdc !important;
    border-radius: 6px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);

    &:last-child {
      margin-bottom: 0;
      border-right: 1px solid #dcdcdc;
    }

    &.active {
      box-shadow: 0 3px 8px rgba(254, 50, 49, 0.3);
    }
  }

  .service-title {
    margin-bottom: 0;
  }

  .service-en-title {
    display: none; /* 移动端隐藏英文标题 */
  }
}

@media (max-width: 480px) {
  .services-container {
    margin-top: 30px;
  }

  .tit-line {
    margin-bottom: 12px;
  }

  .tit-line1 {
    width: 50px;
  }

  .tit-line2 {
    width: 8px;
    margin-left: 8px;
  }

  .solve-tit {
    font-size: 20px;
    margin-bottom: 8px;
  }

  .services-layout {
    margin-top: 15px;
  }
}
</style>
